/**Copyright (c) 2010, Isidoro Trevino de la Garza (chololo@gmail.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
(1)Redistributions of source code must retain the above copyright notice, this list 
of conditions and the following disclaimer. 
(2)Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other 
materials provided with the distribution. 
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
package org.citsa.graphx.concept

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.border.BevelBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;


import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.swing.handler.mxCellHandler;
import com.mxgraph.swing.handler.mxGraphTransferHandler;
import com.mxgraph.swing.util.mxGraphTransferable;
import com.mxgraph.view.mxGraph;



class JTableRenderer extends JComponent {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2106746763664760745L;
	
	/**
	 * 
	 */
	public static final String IMAGE_PATH = "/com/mxgraph/examples/swing/images/";
	
	/**
	 * 
	 */
	protected static JTableRenderer dragSource = null;
	
	/**
	 * 
	 */
	protected static int sourceRow = 0;
	
	/**
	 * 
	 */
	protected Object cell;
	
	/**
	 * 
	 */
	protected def graphContainer;
	
	/**
	 * 
	 */
	protected def graph;
	
	/**
	 * 
	 */
	public JTable table;
	
	/**
	 * 
	 */
	@SuppressWarnings("serial")
	public JTableRenderer(final Object cell,
	final mxGraphComponent graphContainer) {
		this.cell = cell;
		this.graphContainer = graphContainer;
		this.graph = graphContainer.getGraph();
		setLayout(new BorderLayout());
		/*setBorder(BorderFactory.createCompoundBorder(ShadowBorder.getSharedInstance(), BorderFactory
		 .createBevelBorder(BevelBorder.RAISED)));*/
		
		JPanel title = new JPanel();
		title.setBackground(new Color(149, 173, 239));
		title.setOpaque(true);
		title.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 1));
		title.setLayout(new BorderLayout());
		
		/*JLabel icon = new JLabel(new ImageIcon(JTableRenderer.class
		 .getResource(IMAGE_PATH + "preferences.gif")))
		 icon.setBorder(BorderFactory.createEmptyBorder(0, 2, 0, 1));
		 title.add(icon, BorderLayout.WEST);*/
		
		JLabel label = new JLabel(String.valueOf(graph.getLabel(cell)));
		label.setForeground(Color.WHITE);
		label.setFont(title.getFont().deriveFont(Font.BOLD, 11));
		label.setBorder(BorderFactory.createEmptyBorder(0, 1, 0, 2));
		title.add(label, BorderLayout.CENTER);
		
		JPanel toolBar2 = new JPanel();
		toolBar2.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 2));
		toolBar2.setOpaque(false);
		JButton button = new JButton();
		button.setPreferredSize(new Dimension(16, 16));
		button.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		button.setToolTipText("Collapse/Expand");
		button.setOpaque(false);
		toolBar2.add(button);
		
		title.add(toolBar2, BorderLayout.EAST);
		add(title, BorderLayout.NORTH);
		
		// CellStyle style =
		// graph.getStylesheet().getCellStyle(graph.getModel(),
		// cell);
		// if (style.getStyleClass() == null) {
		table = new MyTable();
		
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		
		if (graph.getModel().getChildCount(cell) == 0)
		{
			scrollPane.getViewport().setBackground(Color.WHITE);
			setOpaque(true);
			add(scrollPane, BorderLayout.CENTER);
		}
		
		
		
		
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.add(label, BorderLayout.EAST);
		
		add(panel, BorderLayout.SOUTH);
		
		/*ResizeHandler resizeHandler = new ResizeHandler();
		label.addMouseListener(resizeHandler);
		label.addMouseMotionListener(resizeHandler);
		*/
		setMinimumSize(new Dimension(20, 30));
	}
	
	/**
	 * Implements an event redirector for the specified handle index, where 0
	 * is the top right, and 1-7 are the top center, rop right, middle left,
	 * middle right, bottom left, bottom center and bottom right, respectively.
	 * Default index is 7 (bottom right).
	 *//*
	public class ResizeHandler implements MouseListener, MouseMotionListener {
		
		protected int index;
		
		public ResizeHandler() {
			this(7);
		}
		
		public ResizeHandler(int index) {
			this.index = index;
		}
		
		public void mouseClicked(MouseEvent e) {
			// ignore
		}
		
		public void mouseEntered(MouseEvent e) {
			// ignore
		}
		
		public void mouseExited(MouseEvent e) {
			// ignore
		}
		
		public void mousePressed(MouseEvent e) {
			// Selects to create a handler for resizing
			if (!graph.isCellSelected(cell))
			{
				graphContainer.selectCellForEvent(cell, e);
			}
			
			// Initiates a resize event in the handler
			mxCellHandler handler = graphContainer.getSubHandler().getHandler(
					cell);
			
			if (handler != null)
			{
				// Starts the resize at index 7 (bottom right)
				handler.start(SwingUtilities.convertMouseEvent((Component) e
						.getSource(), e, graphContainer.getGraphControl()),
						index);
				e.consume();
			}
		}
		
		public void mouseReleased(MouseEvent e) {
			graphContainer.getGraphControl().dispatchEvent(
			SwingUtilities.convertMouseEvent((Component) e.getSource(),
			e, graphContainer.getGraphControl()));
		}
		
		public void mouseDragged(MouseEvent e) {
			graphContainer.getGraphControl().dispatchEvent(
			SwingUtilities.convertMouseEvent((Component) e.getSource(),
			e, graphContainer.getGraphControl()));
		}
		
		public void mouseMoved(MouseEvent e) {
			// ignore
		}
	}
	*/
	
	public static JTableRenderer getVertex(Component component) {
		while (component != null) {
			if (component instanceof JTableRenderer) {
				return (JTableRenderer) component;
			}
			component = component.getParent();
		}
		
		return null;
	}
	
	def methodMissing(String name, args) {
		println "MISSING ${name} ${args}"
	}
}
